import 'package:dss_base/utils/log_util.dart';
import 'package:easy_refresh/easy_refresh.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

import 'base_page_view_model.dart';
import 'base_state.dart';
import 'state_holder.dart';

abstract class BasePageState<
  T extends StatefulWidget,
  VM extends BasePageViewModel
>
    extends BaseState<T, VM> {
  /// 单独的页面，往往需要标题区域
  PreferredSizeWidget? onCreateAppbar(BuildContext context) => null;

  /// 底部，不根据状态变化的固定区域
  Widget? onCreateBottom(BuildContext context) => null;

  /// 页面状态 - 初始化
  Widget onCreateInitPage(BuildContext context) =>
      PageStateHolder.instance.initStatePage;

  /// 页面状态 - 加载中
  Widget onCreateLoadingPage(BuildContext context) =>
      PageStateHolder.instance.loadingStatePage;

  /// 页面状态 - 加载完成后的内容
  Widget onCreateSuccessPage(BuildContext context, ScrollPhysics? physics);

  /// 页面状态 - 加载失败
  Widget onCreateFailurePage(BuildContext context) =>
      PageStateHolder.instance.createFailureStatePage();

  /// 页面状态 - 空数据
  Widget onCreateEmptyPage(BuildContext context) =>
      PageStateHolder.instance.createEmptyStatePage();

  /// 当下拉刷新时调用
  Future<IndicatorResult> onRefresh() async {
    return await viewModel.refresh();
  }

  /// 当加载更多时调用
  Future<IndicatorResult> onLoadMore() async {
    return await viewModel.loadMore();
  }

  bool isEnableRefresh() => true;

  bool isEnableLoadMore() => false;

  @override
  void initData() {
    super.initData();
    viewModel.initData();
  }

  @override
  Widget onBuild(BuildContext context) {
    PreferredSizeWidget? appBar = onCreateAppbar(context);
    Widget body;
    if (isEnableRefresh() || isEnableLoadMore()) {
      body = EasyRefresh.builder(
        onRefresh: isEnableRefresh() ? onRefresh : null,
        onLoad: isEnableLoadMore() ? onLoadMore : null,
        childBuilder: (context, physics) {
          return _getWidgetByState(
            context,
            viewModel.pageState,
            physics: physics,
          );
        },
      );
    } else {
      body = _getWidgetByState(context, viewModel.pageState, physics: null);
    }

    Widget? bottom = onCreateBottom(context);
    if(bottom != null){
      body = Column(
        children: [
          Expanded(child: body),
          bottom,
        ],
      );
    }

    return Scaffold(
      appBar: appBar,
      body: SafeArea(child: body),
    );
  }

  /// 根据状态来显示当前的 Widget
  Widget _getWidgetByState(
    BuildContext context,
    PageState state, {
    ScrollPhysics? physics,
  }) {
    Widget page;
    switch (state) {
      case PageState.initState:
        page = onCreateInitPage(context);
        break;
      case PageState.loadingState:
        page = onCreateLoadingPage(context);
        break;
      case PageState.successState:
        page = onCreateSuccessPage(context, physics);
        break;
      case PageState.failureState:
        page = onCreateFailurePage(context);
        break;
      case PageState.emptyState:
        page = onCreateEmptyPage(context);
        break;
    }
    if (isEnableRefresh() && state != PageState.successState) {
      double appBarHeight = Scaffold.of(context).appBarMaxHeight ?? 0;
      double screenHeight = MediaQuery.of(context).size.height;
      double safeAreaTop = MediaQuery.of(context).padding.top;
      double safeAreaBottom = MediaQuery.of(context).padding.bottom;
      double minHeight =
          screenHeight - safeAreaTop - safeAreaBottom - appBarHeight;
      page = SingleChildScrollView(
        physics: physics,
        child: Container(
          constraints: BoxConstraints(minHeight: minHeight),
          alignment: Alignment.center,
          child: page,
        ),
      );
    }
    return page;
  }
}
